package com.ruoyi.biz.report.letter.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.ruoyi.biz.category.domain.BizCategoryVO;
import com.ruoyi.biz.category.mapper.BizCategoryMapper;
import com.ruoyi.biz.letter.mapper.*;
import com.ruoyi.biz.report.domain.BizReportConfig;
import com.ruoyi.biz.report.letter.vo.*;
import com.ruoyi.biz.report.mapper.BizReportConfigMapper;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.mapper.SysDeptMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Fei.Cong
 * @date 2024-08-01 10:57
 */
@Service
@RequiredArgsConstructor
public class CategoryReportService {

    private final BizCategoryMapper bizCategoryMapper;
    private final BizLeaderMailboxMapper bizLeaderMailboxMapper;
    private final BizMessageBoardMapper bizMessageBoardMapper;
    private final BizProvinceLetterMapper bizProvinceLetterMapper;
    private final BizReportConfigMapper bizReportConfigMapper;
    private final CategoryService categoryService;

    public List<LeaderMailboxReportResp> getMailboxReport(CategoryReportReq reportReq) {
        if (StrUtil.isBlank(reportReq.getBeginDate())) {
            reportReq.setBeginDate(DateUtil.format(DateUtil.beginOfYear(new Date()), "yyyy-MM"));
        }
        if (StrUtil.isBlank(reportReq.getEndDate())) {
            reportReq.setEndDate(DateUtil.format(DateUtil.endOfYear(new Date()), "yyyy-MM"));
        }
        List<BizCategoryVO> bizCategoryVOList = bizCategoryMapper.selectCategoryReport();
        List<LeaderMailboxReportResp> reportRespList = bizLeaderMailboxMapper.selectReport(reportReq);
        List<LeaderMailboxReportResp> reportResps = Lists.newLinkedList();
        for (BizCategoryVO bizCategoryVO : bizCategoryVOList) {
            LeaderMailboxReportResp reportResp = new LeaderMailboxReportResp();
            Optional<LeaderMailboxReportResp> respOptional = reportRespList.stream().filter(resp -> Objects.equals(resp.getFirstCategoryId(), bizCategoryVO.getFirstCategoryId())
                    && Objects.equals(resp.getSecondCategoryId(), bizCategoryVO.getSecondCategoryId())).findFirst();
            if (respOptional.isPresent()) {
                BeanUtils.copyProperties(respOptional.get(), reportResp);
            }

            reportResp.setFirstCategoryName(bizCategoryVO.getFirstCategoryName());
            reportResp.setSecondCategoryName(bizCategoryVO.getSecondCategoryName());
            reportResps.add(reportResp);
        }
        getLastNotCategory(reportRespList, reportResps);
        return reportResps;
    }

    private void getLastNotCategory(List<LeaderMailboxReportResp> reportRespList, List<LeaderMailboxReportResp> reportResps) {
        LeaderMailboxReportResp reportResp = new LeaderMailboxReportResp();
        for (LeaderMailboxReportResp resp : reportRespList) {
            if (Objects.equals(resp.getFirstCategoryId(), 0L)
                    && Objects.equals(resp.getSecondCategoryId(), 0L)) {
                BeanUtils.copyProperties(resp, reportResp);
                break;
            }
        }
        reportResp.setFirstCategoryName("未分类");
        reportResp.setSecondCategoryName("未分类");
        reportResps.add(reportResp);
    }

    public List<ReportYearCategoryResp> getMailboxReportGroupByMonthAndCategory(CategoryReportReq reportReq) {
        List<ReportYearCategoryResp> hotlineReportResps = bizLeaderMailboxMapper.selectReportGroupMonthAndCategory(reportReq);
        return buildMonthAndCategoryData(hotlineReportResps);
    }

    public List<ReportYearCategoryResp> getMessageBoardReportGroupByMonthAndCategory(CategoryReportReq reportReq) {
        List<ReportYearCategoryResp> hotlineReportResps = bizMessageBoardMapper.selectReportGroupMonthAndCategory(reportReq);
        return buildMonthAndCategoryData(hotlineReportResps);
    }

    public List<ReportYearCategoryResp> getProvinceReportGroupByMonthAndCategory(CategoryReportReq reportReq) {
        List<ReportYearCategoryResp> hotlineReportResps = bizProvinceLetterMapper.selectReportGroupMonthAndCategory(reportReq);
        return buildMonthAndCategoryData(hotlineReportResps);
    }

    private List<ReportYearCategoryResp> buildMonthAndCategoryData(List<ReportYearCategoryResp> hotlineReportResps) {
        List<ReportYearCategoryResp> hotlineReportRespList = Lists.newLinkedList();
        List<BizReportConfig> reportConfigList = bizReportConfigMapper.selectList(new LambdaQueryWrapper<>());
        List<BizCategoryVO> bizCategoryVOList = bizCategoryMapper.selectCategoryReport();
        for (BizCategoryVO bizCategoryVO : bizCategoryVOList) {
            for (BizReportConfig bizReportConfig : reportConfigList) {
                ReportYearCategoryResp hotlineReportResp = new ReportYearCategoryResp();
                hotlineReportResp.setFirstCategoryId(bizCategoryVO.getFirstCategoryId());
                hotlineReportResp.setSecondCategoryId(bizCategoryVO.getSecondCategoryId());
                hotlineReportResp.setFirstCategoryName(bizCategoryVO.getFirstCategoryName());
                hotlineReportResp.setSecondCategoryName(Objects.isNull(bizCategoryVO.getSecondCategoryName()) ? "" : bizCategoryVO.getSecondCategoryName());
                hotlineReportResp.setBillMonth(bizReportConfig.getBillMonth());
                for (ReportYearCategoryResp hotlineReport : hotlineReportResps) {
                    if (hotlineReport.getFirstCategoryId().equals(bizCategoryVO.getFirstCategoryId())
                            && hotlineReport.getSecondCategoryId().equals(bizCategoryVO.getSecondCategoryId()) && Objects.equals(hotlineReport.getBillMonth(), bizReportConfig.getBillMonth())) {
                        hotlineReportResp.setCnt(hotlineReport.getCnt());
                    }
                }
                hotlineReportRespList.add(hotlineReportResp);
            }
        }
        return hotlineReportRespList;
    }

    /**
     * 热线，月份  分类统计
     */
    public List<ReportYearCategoryResp> getReportGroupByMonthAndCategory(CategoryReportReq reportReq) {
        List<ReportYearCategoryResp> hotlineReportResps = Lists.newArrayList();
        switch (reportReq.getType()) {
            case 1:
                hotlineReportResps = bizLeaderMailboxMapper.selectReportGroupMonthAndCategory(reportReq);
                break;
            case 2:
                hotlineReportResps = bizMessageBoardMapper.selectReportGroupMonthAndCategory(reportReq);
                break;
            case 3:
                hotlineReportResps = bizProvinceLetterMapper.selectReportGroupMonthAndCategory(reportReq);
                break;
            default:
                break;
        }
        return categoryService.getReportMonthAndCategory(hotlineReportResps, reportReq);
    }
}
